class Solution:
    def calculate(self, s: str) -> int:
        n, operand = 0, 0
        stack = []
        for i in range(len(s)-1, -1, -1):
            char = s[i]
            if char == "(":
                res += self.calc(stack)
                stack.pop()
                stack.append(res)
            elif char.isdigit():
                operand +=  (10 ** n) * int(char) 
                n += 1
            elif char == '+' or char == '-':
                stack.append(char)
                n = 0
            else:
                n = 0

        if n:
            stack.append(operand)
            
        return self.calc(stack)
                
        
    def calc(self, stack) -> int:
        print(stack)
        res = stack.pop()
        while stack and stack[-1] != ')':
            char = stack.pop()

            if char == '+':
                res += stack.pop()
            elif char == '-':
                res -= stack.pop()
        return res


